package algorithms.leecode;

import java.util.*;

public class Au46 {
    public static void main(String[] args) {
        int[] nums = {1,1,3,4};
        permute(nums);
    }

    public static List<List<Integer>> permute(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> resList = new ArrayList<>();
        Set<Integer> set = new HashSet<>();
        List<Integer>list = new ArrayList<>();
        f(resList,nums,list,set);
        System.err.println(resList);
        return resList;

    }

    public static void f(List<List<Integer>> resList, int[] nums, List<Integer> list, Set<Integer> set) {
        if (list.size() == nums.length) {
            resList.add(list);

        }
        for (int i = 0; i < nums.length; i++) {
            if (set.contains(i)) {
                continue;
            }

            if (i > 0) {
                if (nums[i] == nums[i - 1] && set.contains(i - 1)) {
                    continue;
                }
            }
            List<Integer> tmp = new ArrayList<>();
            tmp.addAll(list);
            tmp.add(nums[i]);
            set.add(i);
            f(resList,nums,tmp,set);
            set.remove(i);
        }

    }
}
